package com.fpc.cn.springbootutil.user;

import com.fpc.cn.springbootutil.base.BaseResponse;
import com.fpc.cn.springbootutil.base.BaseUtilService;
import com.fpc.cn.springbootutil.common.DbBatchUtil;
import com.fpc.cn.springbootutil.common.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class UserService {

    @Autowired
    private UserMapeer baseMapper;

    @Autowired
    private BaseUtilService baseUtilService;


    public List<Map<String, Object>> selectList(User user) {
        return baseMapper.selectList(user);
    }


    public BaseResponse createUser(User user) {
        user.setId(StringUtil.getUUID());
        baseMapper.insert(user);

       return baseUtilService.setResultSuccess();
    }

    public BaseResponse bathCreateUser(User user) {

        List<User> listUser = new ArrayList<>();
        for (int i = 0; i <20 ; i++) {
            User userBean = new User();
            userBean.setId( StringUtil.getUUID());
            userBean.setAge(user.getAge());
            userBean.setUserName(user.getUserName()+""+i);
            listUser.add(userBean);
        }

        // 使用线程批量插入
        new DbBatchUtil<UserMapeer, User>()
                .batch(listUser, UserMapeer::insert);

        return baseUtilService.setResultSuccess();
    }


}